package com.googlecode.dgwt.client.dojo.data.model;

import com.google.gwt.dom.client.Element;
import com.googlecode.dgwt.client.dijit.View;
import com.googlecode.dgwt.client.dojo.Dojo;
import com.googlecode.dgwt.client.dojo.exception.ValidationException;


/**
 * @author      Wellington Tadeu dos Santos
 * @since       DGWT v0.01
 */
public abstract class TableColumnBase<Item, Type> extends StoreColumnBase<Item, Type> implements TableColumn<Item, Type> {

    private String title;
    private String width;

    public TableColumnBase() {
        super();
        title = "";
    }

    public TableColumnBase(String title) {
        super();
        setTitle(title);
    }

    @Override
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        if (title == null) {
            title = "";
        }
        if (!title.equals(this.title)) {
            this.title = title;
            onColumnChanged();
        }
    }

    public void setWidth(String width) {
        if (width == null){
            width = "";
        }
        this.width = width.trim();
    }

    @Override
    public String getWidth() {
        return width;
    }

    @Override
    public void setDisplayValue(Item item, String value) throws ValidationException {
        try {
            super.setDisplayValue(item, value);
        } catch (ValidationException e) {
            throw new ValidationException(Dojo.string.propertiesSubstitute(e.getMessage(), getTitle()));
        }
    }

    @Override
    public void onUpdateHeader(View view, Element element) {
        element.setInnerText(getTitle());
    }

    @Override
    public void onFormatHeader(Element element) {
    }

}
